import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.header import Header

"""
smtplip模块
	--连接邮箱服务器
	--登录邮箱
	--发送邮件
"""

sendEmail = "772084279@qq.com"
toAddrEmail = ["772084279@qq.com"]


#  发送图片
imageFile = "性能测试-王勇20200818.png"
image = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
image.add_header('Content-Disposition', 'attachment', filename=imageFile)

# 发送HTML附件
htmlFile = "自动化回归_2020-10-24_23_43_11_result.html"
html = MIMEApplication(open(htmlFile, 'rb').read())
html.add_header('Content-Disposition', 'attachment', filename=htmlFile)

# 发送内容为HTML
HTML_content = MIMEText(open(htmlFile, 'rb').read(), 'html', 'utf-8')

# 发送文本邮件
text = "我是python脚本发送的邮件！！"
msg = MIMEText(text, 'plain', 'utf-8')

m = MIMEMultipart()
m.attach(html)
m.attach(image)
m.attach(HTML_content)
m.attach(msg)

m["From"] = sendEmail
m["Subject"] = "python自动化"

try:
    server = smtplib.SMTP()
    server.connect("smtp.qq.com", 25)
    server.login(sendEmail, "ulyfpbfuuwqebeid")
    server.sendmail(sendEmail, toAddrEmail, m.as_string())
    server.quit()
    print("邮件发送成功！")
except Exception as e:
    print(e)
